namespace Code1
{
    using System;
    using System.Text;
    using System.Web;
    using System.Data;
    using System.ComponentModel;
    using MySql.Data.MySqlClient;
    using System.Collections;
    using CodeFx;
    using CodeFx.Web;
	using System.Collections.Generic;
    using System.Configuration;
    using System.Configuration.Provider;
		
    [DataObject]
	public static partial class MailDTO
	{
		private static MySqlConnection OpenConnection
        {
            get
            {
                ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings["Code1"];
                if (ConnectionStringSettings == null || ConnectionStringSettings.ConnectionString.Trim().Length == 0)
                    throw new ProviderException("Connection string cannot be blank.");
                MySqlConnection conn = new MySqlConnection(ConnectionStringSettings.ConnectionString);
                conn.Open();
                return conn;
            }
        }

		#region DTO Method
		
		#region Select
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
		public static MailDAL Select(uint _IdMail)
        {
				const string command = "select * from  mail where IdMail =?IdMail";

				
            	MySqlConnection conn = OpenConnection;        
				
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
cmd.Parameters.Add(new MySqlParameter("?IdMail", _IdMail));
				MailDAL dataobject = null;
				try
				{
					dataobject = Select(cmd);
				}
				finally
				{
					conn.Close();
				}
           	 	return dataobject;
        }
		
		#endregion
		
		#region SelectbyFk
		 [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
		public static List<MailDAL> SelectByFk_ToMail(string ToMail)
        {
				const string command = "select * from  mail where ToMail =?ToMail";

				MySqlConnection conn = OpenConnection;        
				
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
cmd.Parameters.Add(new MySqlParameter("?ToMail", ToMail));
			List<MailDAL> al= null;
				try
				{
					al = SelectAll(cmd);
				}
				finally
				{
					conn.Close();
				}
           	 	return al;
        }
		#endregion
		
		#region SelectAll
		[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public static List<MailDAL> SelectAll()
        {
			MySqlConnection conn = OpenConnection;        
				
			MySqlCommand cmd = new MySqlCommand("select * from mail", conn);
			List<MailDAL> al= null;
			try
			{
				al = SelectAll(cmd);
			}
			finally
			{
				conn.Close();
			}
       	 	return al;
        }
		#endregion
		
		#region Delete
		[DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public static void Delete(MailDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
			
			const string command = "delete from mail where IdMail =?IdMail";

				
				MySqlConnection conn = OpenConnection;        
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
cmd.Parameters.Add(new MySqlParameter("?IdMail", dataObject.IdMail));
			
            	try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw new DALException(command, dataObject, ex.InnerException);
                }
				finally
				{
					conn.Close();
				}
        }
		#endregion
  
		#region ByFkDelete
		[DataObjectMethodAttribute(DataObjectMethodType.Delete, false)]
        public static void DeleteByFk_ToMail(MailDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
			
			const string command = "delete from mail where ToMail =?ToMail";

				MySqlConnection conn = OpenConnection;        
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
cmd.Parameters.Add(new MySqlParameter("?ToMail", dataObject.ToMail));
			
		
            	try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw new DALException(command, dataObject, ex.InnerException);
                }
				finally
				{
					conn.Close();
				}
		}
		#endregion
		
		#region Insert
   		[DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public static void Insert(MailDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
				
			const string command = "insert into mail (FromMail,ToMail,Sujet,Body,Date) values ( ?FromMail,?ToMail,?Sujet,?Body,?Date)";

				MySqlConnection conn = OpenConnection;        
				
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
            	try
                {
                    dataObject.Write(cmd);
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw new DALException(command, dataObject, ex.InnerException);
                }
				finally
				{
					conn.Close();
				}
        }
		#endregion
		
		#region LastId
		public static uint GetLastId(MailDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
            const string command = "SELECT  IdMail from mail  where ToMail= ?ToMail order by IdMail desc limit 0,1";
						
				MySqlConnection conn = OpenConnection;    
			MySqlCommand cmd = new MySqlCommand(command, conn);
					cmd.Parameters.Add(new MySqlParameter("?ToMail", dataObject.ToMail));
			
            	try
                {
                	object id = cmd.ExecuteScalar();
                	if ((id != null) && (id is uint))
                		return (uint)id;
					else
						return 0;
				}
                catch (Exception ex)
                {
                    throw new DALException(command, dataObject, ex.InnerException);
                }
				finally
				{
					conn.Close();
				}
        }	
		#endregion
		
		#region Update
		[DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public static void Update(MailDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
            const string command = "update mail set FromMail = ?FromMail,ToMail = ?ToMail,Sujet = ?Sujet,Body = ?Body,Date = ?Date where IdMail = ?IdMail";
				MySqlConnection conn = OpenConnection;  
            MySqlCommand cmd = new MySqlCommand(command, conn); 
			try
            {
                dataObject.Write(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new DALException(command, dataObject, ex.InnerException);
            }
			finally
			{
				conn.Close();
			}
        }
		#endregion
		
		#region Exist		
		public static bool Exist(uint _IdMail)
        {
           	const string command = "select 1 from mail where IdMail =?IdMail";

				MySqlConnection conn = OpenConnection;  
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
cmd.Parameters.Add(new MySqlParameter("?IdMail", _IdMail));
				
            try
            {
            	return (cmd.ExecuteScalar() != null);
			}
            catch (Exception ex)
            {
                throw new DALException(command, ex.InnerException);
            }
			finally
			{
				conn.Close();
			}
        }

		public static bool Exist(MailDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
				
           	const string command = "select 1 from mail where IdMail =?IdMail and FromMail =?FromMail and ToMail =?ToMail and Sujet =?Sujet and Body =?Body and Date =?Date";

				MySqlConnection conn = OpenConnection;  
				MySqlCommand cmd = new MySqlCommand(command, conn);
				
cmd.Parameters.Add(new MySqlParameter("?IdMail", dataObject.IdMail));
cmd.Parameters.Add(new MySqlParameter("?FromMail", dataObject.FromMail));
cmd.Parameters.Add(new MySqlParameter("?ToMail", dataObject.ToMail));
cmd.Parameters.Add(new MySqlParameter("?Sujet", dataObject.Sujet));
cmd.Parameters.Add(new MySqlParameter("?Body", dataObject.Body));
cmd.Parameters.Add(new MySqlParameter("?Date", dataObject.Date));
            try
            {
            	return (cmd.ExecuteScalar() != null);
			}
            catch (Exception ex)
            {
                throw new DALException(command, dataObject, ex.InnerException);
            }
			finally
			{
				conn.Close();
			}

        }
        #endregion	
		
		#endregion	
	
		#region Public methods
		public static MailDAL Select(MySqlCommand cmd)
        {
            if (cmd == null)
                throw new ArgumentNullException("cmd");
            	MailDAL dataObject = null;
				MySqlDataReader dr = null;
				try{
					dr = cmd.ExecuteReader(CommandBehavior.SingleResult);
                	if (dr.Read())
					{
						dataObject = new MailDAL();
						dataObject.Read(dr);
					}
				}
				catch (Exception ex)
				{
					throw new DALException(cmd.CommandText,ex.InnerException);
				}
				finally{
				if (dr != null)
					dr.Close();
				}
            	return dataObject;
        }
		
		public static List<MailDAL> SelectAll(MySqlCommand cmd)
        {
            if (cmd == null)
                throw new ArgumentNullException("cmd");
				List<MailDAL> al = null;
            	MySqlDataReader dr = null;
				try{
					dr = cmd.ExecuteReader();
					if (dr.HasRows)
					{
                    	al = new List<MailDAL>();
						while (dr.Read())
						{
							MailDAL dataObject = new MailDAL();
							dataObject.Read(dr);
							al.Add(dataObject);
						}
						al.TrimExcess();
					}
				}
				catch (Exception ex)
				{
					throw new DALException(cmd.CommandText,ex.InnerException);
				}
				finally{
				if (dr != null)
					dr.Close();
				}
            	return al;
        }
        
		#endregion
	
		#region WithPersistance
		
		[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public static MailDAL SelectWithPersistance(uint _IdMail)
        {
            return PersistanceMail.Current.GetObject(_IdMail.ToString());
        }
		
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public static MailDAL SelectWithPersistance()
        {
            return PersistanceMail.Current.Object;
        }
		
		[DataObjectMethodAttribute(DataObjectMethodType.Delete, false)]
        public static void DeleteWithPersistance(MailDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
            Delete(dataObject);
            PersistanceMail.Current.RemoveObject(dataObject.IdMail.ToString());
        }
		
		[DataObjectMethodAttribute(DataObjectMethodType.Update, false)]
        public static void UpdateWithPersistance(MailDAL dataObject)
        {
            if (dataObject == null)
                throw new ArgumentNullException("dataObject");
            Update(dataObject);
            PersistanceMail.Current.SetObject(dataObject);
        }
		
		[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public static List<MailDAL>SelectAllWithPersistance()
        {
			List<MailDAL> al = PersistanceMails.Current.Object;
			if (al == null)
			{
				al = SelectAll();
				PersistanceMails.Current.SetObject(al);
			}
			return al;
    
        }
		#endregion
	}
}